﻿using BaseUI;
using BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.UI.WebControls;

namespace Thksoft.BusinessExpansion.Process
{
    public partial class SetMonthQueue : BasePage
    {
        private Sys_CompanyBLL combll = new Sys_CompanyBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadCompanyData();
            }
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        protected void Bind(string company_id)
        {
            DataTable data;
            var sql = string.Format(@"select '' USERNAME,cq.id as MONTH_QUEUE_ID,cq.company_id,c.cmy_name AS CMY_NAME,p.n_process_id,p.c_process_name AS PROCESS_NAME,cq.queueid,n.c_node_name AS NODE_NAME, decode(q.n_audit_type, 0, '单人办理', 1, '多人审核1人通过即通过', 2, '多人审核全部通过才通过', 3, '人员办理', 4, '默认为片区管理员', 5, '指定特定操作产生的人员') AUTO_TYPE,cq.sysuser_id from lc_queue q,lc_comqueue cq,sys_company c,lc_node n,lc_process p where q.n_queue_id=cq.queueid and cq.company_id=c.cmy_id and q.n_node_id=n.n_node_id and q.n_process_id=p.n_process_id and cq.company_id=" + company_id + " order by cq.id");
            data = new BaseBLL().Query(sql);
            List<string> userids = new List<string>();
            foreach (DataRow item in data.Rows)
            {
                if (item["sysuser_id"] != DBNull.Value)
                {
                    userids.AddRange(item["sysuser_id"].ToString().Split(','));
                }
            }
            var Chid = "";
            userids = userids.Where(a => a.Trim() != "").ToList();
            userids = userids.Distinct().ToList();
            if (userids.Count != 0)
            {
                for (int i = 0; i < userids.Count; i++)
                {
                    if (i + 1 != userids.Count)
                    {
                        Chid += userids[i] + ",";
                    }
                    else
                    {
                        Chid += userids[i];
                    }
                }
                sql = "SELECT USER_ID,USER_NAME FROM THKQS.SYS_USER WHERE USER_ID IN(" + Chid + ")";
                DataTable mdata = new BaseBLL().Query(sql);
                for (int i = 0; i < data.Rows.Count; i++)
                {
                    if (data.Rows[i]["sysuser_id"] != DBNull.Value)
                    {
                        var str = "";
                        var xx1 = data.Rows[i]["sysuser_id"].ToString().Split(',').Where(a => a.Trim() != "").ToList();
                        foreach (var item in xx1)
                        {
                            bool ifstop = false;
                            for (int k = 0; k < mdata.Rows.Count && ifstop == false; k++)
                            {
                                if (mdata.Rows[k]["USER_ID"].ToString() == item)
                                {
                                    str += mdata.Rows[k]["USER_NAME"] + ",";
                                    ifstop = true;
                                }
                            }
                        }
                        if (str.LastIndexOf(",") == (str.Length - 1) && str.Length > 0)
                        {
                            str = str.Substring(0, str.Length - 1);
                        }
                        data.Rows[i]["USERNAME"] = str;
                    }
                }
            }
            PageIng.Visible = true;
            PageIng.RowsCount = data.Rows.Count;
            PageIng.DataSource = data;
            PageIng.GridViewSource = grvShow;
            PageIng.RowsPerPage = PageIng._rowsPerPage;
            PageIng.Bind();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSelect_Click(object sender, EventArgs e)
        {
            if (Company.SelectedValue.ToString() == "-1")
            {
                MessageBox.Show(Page, "请选择公司");
                return;
            }
            Bind(Company.SelectedValue.ToString());
        }

        protected void LoadCompanyData()
        {
            this.Company.DataSource = new Sys_CompanyBLL().GetInfoByPId((int)CurrentUserCompanyID);
            this.Company.DataValueField = "CMY_ID";
            this.Company.DataTextField = "CMY_NAME";
            this.Company.DataBind();
            ListItem item1 = new ListItem("---请选择---", "-1");
            this.Company.Items.Insert(0, item1);
            this.Company.SelectedValue = "-1";
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnInsert_Click(object sender, EventArgs e)
        {
            MessageBox.ResponseScript(this, "FORM_INSERT();");
            //Response.Redirect("InsertMonthQueue.aspx");
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnEdit_Click(object sender, EventArgs e)
        {
            int count = 0;
            for (int i = 0; i < grvShow.Rows.Count; i++)
            {
                if (((CheckBox)grvShow.Rows[i].FindControl("chkSelectId")).Checked == true)
                {
                    count++;
                    if (count == 1)
                    {
                        string pid = ((CheckBox)grvShow.Rows[i].FindControl("chkSelectId")).ToolTip;
                        MessageBox.ResponseScript(this, "FORM_EDIT(\"" + pid + "\");");
                        //string url = "InsertMonthQueue.aspx?ID=" + pid;
                        //Response.Redirect(url);
                        return;
                    }
                }
            }
            MessageBox.Show(this, "请选择需要编辑的项！");
        }

        protected void grvShow_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "cu=style.backgroundColor;style.backgroundColor='#d5e7ff'");
                e.Row.Attributes.Add("onmouseout", "style.backgroundColor=cu");
            }
        }

        protected void PageIng_DataBinding(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// GridView标题排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grvShow_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sPage = e.SortExpression;
            string SortOrder, OrderDire;
            SortOrder = ViewState["SortOrder"] == null ? null : (string)ViewState["SortOrder"];
            OrderDire = ViewState["OrderDire"] == null ? null : (string)ViewState["OrderDire"];
            if (SortOrder == sPage)
            {
                if (OrderDire == "Desc")
                    OrderDire = "ASC";
                else
                    OrderDire = "Desc";
            }
            else
            {
                SortOrder = e.SortExpression;
                OrderDire = "ASC";
            }
            ViewState["SortOrder"] = SortOrder;
            ViewState["OrderDire"] = OrderDire;
            BindProjectName(true);
        }

        /// <summary>
        /// 绑定项目名称（非动态）绑定GridView
        /// </summary>
        public void BindProjectName(bool iSsort)
        {
            string company_id = Company.SelectedValue.ToString();
            if (company_id == "-1")
            {
                MessageBox.Show(Page, "请选择公司");
                return;
            }

            DataTable data;
            var sql = string.Format(@"select '' USERNAME,cq.id as MONTH_QUEUE_ID,cq.company_id,c.cmy_name AS CMY_NAME,p.n_process_id,p.c_process_name AS PROCESS_NAME,cq.queueid,n.c_node_name AS NODE_NAME, decode(q.n_audit_type, 0, '单人办理', 1, '多人审核1人通过即通过', 2, '多人审核全部通过才通过', 3, '人员办理', 4, '默认为片区管理员', 5, '指定特定操作产生的人员') AUTO_TYPE,cq.sysuser_id from lc_queue q,lc_comqueue cq,sys_company c,lc_node n,lc_process p where q.n_queue_id=cq.queueid and cq.company_id=c.cmy_id and q.n_node_id=n.n_node_id and q.n_process_id=p.n_process_id and cq.company_id=" + company_id + " order by cq.id");
            data = new BaseBLL().Query(sql);
            List<string> userids = new List<string>();
            foreach (DataRow item in data.Rows)
            {
                if (item["sysuser_id"] != DBNull.Value)
                {
                    userids.AddRange(item["sysuser_id"].ToString().Split(','));
                }
            }
            var Chid = "";
            userids = userids.Where(a => a.Trim() != "").ToList();
            userids = userids.Distinct().ToList();
            if (userids.Count != 0)
            {
                for (int i = 0; i < userids.Count; i++)
                {
                    if (i + 1 != userids.Count)
                    {
                        Chid += userids[i] + ",";
                    }
                    else
                    {
                        Chid += userids[i];
                    }
                }
                sql = "SELECT USER_ID,USER_NAME FROM THKQS.SYS_USER WHERE USER_ID IN(" + Chid + ")";
                DataTable mdata = new BaseBLL().Query(sql);
                for (int i = 0; i < data.Rows.Count; i++)
                {
                    if (data.Rows[i]["sysuser_id"] != DBNull.Value)
                    {
                        var str = "";
                        var xx1 = data.Rows[i]["sysuser_id"].ToString().Split(',').Where(a => a.Trim() != "").ToList();
                        foreach (var item in xx1)
                        {
                            bool ifstop = false;
                            for (int k = 0; k < mdata.Rows.Count && ifstop == false; k++)
                            {
                                if (mdata.Rows[k]["USER_ID"].ToString() == item)
                                {
                                    str += mdata.Rows[k]["USER_NAME"] + ",";
                                    ifstop = true;
                                }
                            }
                        }
                        if (str.LastIndexOf(",") == (str.Length - 1) && str.Length > 0)
                        {
                            str = str.Substring(0, str.Length - 1);
                        }
                        data.Rows[i]["USERNAME"] = str;
                    }
                }
            }

            DataView view = data.DefaultView;
            if (iSsort)
            {
                string sort = (string)ViewState["SortOrder"] + " " + (string)ViewState["OrderDire"];
                view.Sort = sort;//设置排序
            }

            grvShow.DataSource = view;

            grvShow.DataBind();
        }
    }
}